package com.fpc.util.fileUtil;

import org.apache.commons.lang3.StringUtils;
//import cn.csg.upt.exception.BusinessException;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.DecimalFormat;

/**
 * @author lft
 * @since 2020-12-21
 * @description 附件 - 文件处理工具
 */
public class FileUtils {

    static final long KB = 1024L;
    static final long MB = 1048576L;
    static final long GB = 1073741824L;
    static final long TB = 1099511627776L;
    static final long PB = 1125899906842624L;
    static final long EB = 1152921504606846976L;
    static final long ZB = 0L;
    static final long YB = 0L;

    /**
     * 获取文件大小（带单位）
     * @param b  文件大小，以字节为单位
     * @return
     */
    public static String getFileSizeName(long b) {
        float bytes = (float)b;
        if (bytes < 1024.0F) {
            return b + "Bytes";
        }
        if (bytes < 1048576.0F) {
            return b / 1024L + "K";
        }

        if (bytes < 1.073742E+009F) {
            return new DecimalFormat("###,###,###,###.#").format(bytes / 1048576.0F) + "M";
        }

        if (bytes < 1.099512E+012F) {
            return new DecimalFormat("###,###,###,###.#").format(bytes / 1.073742E+009F) + "G";
        }

        if (bytes < 1.1259E+015F) {
            return new DecimalFormat("###,###,###,###.#").format(bytes / 1.099512E+012F) + "T";
        }

        if (bytes < 1.152922E+018F) {
            return new DecimalFormat("###,###,###,###.#").format(bytes / 1.1259E+015F) + "P";
        }

        if (bytes < 0.0F) {
            return new DecimalFormat("###,###,###,###.#").format(bytes / 1.152922E+018F) + "E";
        }

        if (bytes < 0.0F) {
            return new DecimalFormat("###,###,###,###.#").format(bytes / 0.0F) + "Z";
        }

        return new DecimalFormat("###,###,###,###.#").format(bytes / 0.0F) + "Y";
    }

    /**
     * 获取真实IP
     * @param request 请求体
     * @return 真实IP
     */
    public static String getRealIp(HttpServletRequest request) {
        // 这个一般是Nginx反向代理设置的参数
        String ip = request.getHeader("X-Real-IP");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        // 处理多IP的情况（只取第一个IP）
        if (ip != null && ip.contains(",")) {
            String[] ipArray = ip.split(",");
            ip = ipArray[0];
        }
        
        return ip;
    }
    /**
     * 获取resource 下文件的相对对路径 /开头   第一种方法
     * @param resourcePath  resource下文件的路径   
     * @return
     */
    public static String getAbsolutePath(String resourcePath) {
    	ClassLoader loader = Thread.currentThread().getContextClassLoader();
        File file = new File(loader.getResource(resourcePath).getFile());
        String path=file.getAbsolutePath();
        if(StringUtils.isBlank(path)) {
        	ClassPathResource classPathResource = null;
            classPathResource = new ClassPathResource(resourcePath);
            URL url= null;
            try {
                url = classPathResource.getURL();
            } catch (IOException e) {
            	throw new RuntimeException("文件没有找到："+resourcePath);
            }
            path=url.getPath();
        }
       return path;
    }
    
    /**  
     * 第二种方法   相对路径 /开头
     *  读取resources资源路径下文件的输入流
     */
    public static InputStream  genInputStreamByFile(String fileUri) throws Exception{
     if (StringUtils.isBlank(fileUri)){
      throw new Exception("文件路径为空！");
     }
     ClassPathResource classPathResource = new ClassPathResource(fileUri);
     InputStream inputStream = classPathResource.getInputStream();
     return inputStream;
    }
    
    /**
     * 
     * @param fileUri  绝对路径 (不要是要new file（） 这种方式来找文件，现在大部分服务器都部署于容器当中，绝对路径会有问题)
     * @return
     * @throws Exception
     */
    public static InputStream  genabsoulpathByFile(String fileUri) throws Exception{
        if (StringUtils.isBlank(fileUri)){
         throw new Exception("文件路径为空！");
        }
        File file = new File(fileUri);
        
        
        return null;
       }

   

}
